<template>
    <div>
        <el-card shadow="never" class="mb-5">
            <div class="flex items-center">
                <el-text>分类名称</el-text>
                <div class="ml-3 w-52 mr-5"><el-input v-model="searchCategoryName" placeholder="请输入（模糊查询）"/></div>

                <el-text>创建日期</el-text>
                <div class="ml-3 w-30 mr-5">
                    <el-date-picker v-model="pickDate" type="daterange" range-separator="至" start-placeholder="开始时间" 
                    end-placeholder="结束时间" :shortcuts="shortcuts" size="default" @change="datepickerChange"/>
                </div>

                <el-button type="primary" class="ml-3" :icon="Search" @click="getTableData">查询</el-button>
                <el-button class="ml-3" :icon="RefreshRight" @click="reset">重置</el-button>
            </div>
        </el-card>

        <el-card shadow="never">
            <!-- 新建按钮 -->
            <div class="mb-5">
                <el-button type="primary" @click="addCategoryBtnClick">
                    <el-icon class="mr-1">
                        <Plus/>
                    </el-icon>
                新增</el-button>
            </div>
            <!-- 分页列表 -->
            <el-table :data="tableData" border stripe style="width: 100%" v-loading="tableLoading">
                <el-table-column prop="name" label="分类名称" width="180"/>
                <el-table-column prop="articlesTotal" label="文章数" width="100"/>
                <el-table-column prop="createTime" label="创建时间" width="180"/>
                <el-table-column label="操作">
                    <template #default="scope">
                        <el-button type="danger" size="small" @click="deleteCategorySubmit(scope.row)">
                            <el-icon class="mr-1">
                                <Delete />
                            </el-icon>
                            删除
                        </el-button>
                    </template>
                </el-table-column> 
            </el-table>
        </el-card>

        <!-- 分页 -->
        <div class="mt-10 flex justify-center">
            <el-pagination v-model:current-page="current" v-model:page-size="size" :page-sizes="[10, 20, 50]" 
            :small="false" :background="true" layout="total, sizes, prev, pager, next, jumper"
            :total="total" @size-change="handleSizeChange" @current-change="getTableData" />
        </div>

        <FromDialog ref="formDialogRef" title="添加文章分类" destroyOnClose @submit="onSubmit">
            <el-form ref="formRef" :rules="rules" :model="form">
                <el-form-item label="分类名称" prop="name" label-width="80px" size="large">
                    <el-input v-model="form.name" placeholder="请输入分类名称" maxlength="20" show-word-limit clearable />
                </el-form-item>
            </el-form>
        </FromDialog>

    </div>
</template>

<script setup>
import { Search, RefreshRight, Plus, Delete } from '@element-plus/icons-vue'
import { reactive, ref } from 'vue'
import moment from 'moment'
import { getCategoryPageList, addCategory, deleteCategory } from '@/api/admin/category'
import { showMessage, showModel } from '@/composables/util'
import FromDialog from '@/components/FormDialog.vue'

const searchCategoryName = ref('')
const pickDate = ref('')

const tableData = ref([])
const current = ref(1)
const total = ref(0)
const size = ref(10)

const startDate = reactive({})
const endDate = reactive({})

const tableLoading = ref(false)

const formDialogRef = ref(null)

const addCategoryBtnClick = () => {
    formDialogRef.value.open()
}

const formRef = ref(null)

const form = reactive({
    name: ''
})

// 校验规则
const rules = {
    name:[
        {
            required: true,
            message: '分类名称不能为空',
            trigger: 'blur'
        },
        {
            min: 1,
            max: 20,
            message: '分类名称字数要求大于 1 个字符，小于 20 个字符',
            trigger: 'blur'
        },
    ]
}

const onSubmit = () => {
        // 先验证 form 表单字段
    formRef.value.validate((valid) => {
        if (!valid) {
            console.log('表单验证不通过')
            return false
        }
    })
    
    formDialogRef.value.showBtnLaoding()

    addCategory(form).then((res) => {
        if(res.success == true){
            showMessage('添加成功')
            form.name = ''
            formDialogRef.value.close()
            getTableData()
        } else {
            let message = res.message
            showMessage(message, 'error')
        }
    })
    .finally(formDialogRef.value.closeBtnLaoding())
}

const deleteCategorySubmit = (row) => {
    showModel('是否确定要删除该分类？').then(() => {
        deleteCategory(row.id).then((res) => {
            if(res.success == true){
                showMessage('删除成功')
                getTableData()
            } else {
                let message = res.message
                showMessage(message, 'error')
            }
        })
    }).catch(() => {
        console.log('取消了')
    })
}

// 重置画面
const reset = () => {
    searchCategoryName.value = ''
    pickDate.value = ''
    startDate.value = null
    endDate.value = null
    getTableData()
}

// 选择时间
const handleSizeChange = (chooseSize) => {
    size.value = chooseSize
    getTableData()
}

const datepickerChange = (e) => {
    startDate.value = moment(e[0]).format('YYYY-MM-DD')
    endDate.value = moment(e[1]).format('YYYY-MM-DD')
}

const shortcuts = [
    {
        text: '最近一周',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3500 * 1000 * 24 * 7)
            return [start, end]
        },
    },
    {
        text: '最近一月',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30)
            return [start, end]
        },
    },
    {
        text: '最近三个月',
        value: () => {
            const end = new Date()
            const start = new Date()
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90)
            return [start, end]
        },
    },
]

// 获取分页信息
function getTableData() {
    tableLoading.value = true
    getCategoryPageList({current: current.value, size: size.value, startDate: startDate.value, endDate: endDate.value, name: searchCategoryName.value})
    .then((res) => {
        if(res.success == true){
            tableData.value = res.data
            current.value = res.current
            size.value = res.size
            total.value = res.total
        }
    })
    .finally(() => tableLoading.value = false)
}

getTableData()
</script>